﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Reflection;
using System.IO;
using Ue.Extensions;

namespace Ue.Components.VerificationCode
{
    public class VerifyBackgroundImage
    {
        public VerifyBackgroundImage(string fileName,params Color[] colours)
        {
            this.Colours = colours;
            
            this.Image = this.GetImageByPhysicalPath(fileName) ?? this.GetImageByResourceStream(fileName);
        }

        public IEnumerable<Color> Colours { get; private set; }

        public Image Image { get; private set; }

        public string Name { get; private set; }

        private Image GetImageByPhysicalPath(string fileName)
        {
            if (!File.Exists(fileName))
            {
                return null;
            }
            try
            {
                return System.Drawing.Image.FromFile(fileName);
            }
            catch
            {
                return null;
            }
        }
        private Image GetImageByResourceStream(string fileName)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            string resourceShortName = string.Format("Resources.{0}.jpg", fileName);
            var fileFullName = assembly.GetManifestResourceFullName(resourceShortName);
            if (string.IsNullOrEmpty(fileFullName))
            {
                return null;
            }

            try
            {
                return System.Drawing.Image.FromStream(assembly.GetManifestResourceStream(fileFullName));
            }
            catch
            {
                return null;
            }
        }
    }
}
